package com.jt.web.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.util.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.JedisCluster;

import java.util.UUID;
@Service(timeout = 3000)
public class DubboUserServiceImpl implements DubboUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public void doRegister(User user) {
        user.setEmail(user.getPhone());
        String md5Pass= DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        userMapper.insert(user);
    }

    @Override
    public String findUserByUP(User user) {
        String md5Pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>(user);
        User userDB=userMapper.selectOne(queryWrapper);

        if (userDB==null){
            return null;
        }
        String ticket= UUID.randomUUID().toString().replace("-", "");
        userDB.setPassword("123456test");
        String json= ObjectMapperUtil.toJSON(userDB);
        int seconds=30*24*60*60;
        jedisCluster.setex(ticket, seconds, json);
        return ticket;

    }

    @Override
    public String doLogin(User user) {
        String md5Pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        User userDB=userMapper.selectOne(new QueryWrapper<>(user));
        String ticket=null;
        if(userDB!=null){
            ticket=UUID.randomUUID().toString().replace("-", "");
            userDB.setPassword("你好");
            String json=ObjectMapperUtil.toJSON(user);
            jedisCluster.setex(ticket, 7*34*60*60, json);

        }
        return ticket;

    }
}
